import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: ming
 * Date: 2008-6-12
 * Time: 14:05:33
 * To change this template use File | Settings | File Templates.
 * XML的数据
 */
public class AJAXXMLServer extends HttpServlet{
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doGet(httpServletRequest, httpServletResponse);
    }

    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        try{
            //修改点1----响应的Content-Type必须是text/xml
            httpServletResponse.setContentType("text/xml;charset=utf-8");

            PrintWriter out = httpServletResponse.getWriter();
            //1.取参数
            String old = httpServletRequest.getParameter("name");
            //修改点2-----返回的数据需要拼装成xml格式
            StringBuilder builder = new StringBuilder();
            builder.append("<message>");
            //2.检查参数是否有问题
            if(old == null || old.length() == 0){
                builder.append("用户名不能为空").append("</message>");
            } else{
                //3.校验操作
                String name = old;

                if(name.equals("wangxingkui")){
                    //4。和传统应用不同之处。这一步需要将用户感兴趣的数据返回给页面段，而不是将一个新的页面发送给用户
                    //写法没有变化，本质发生了改变
                    builder.append("用户名[" + name + "]已经存在，请使用其他用户名").append("</message>");
                } else{
                    builder.append("用户名[" + name + "]尚未存在，可以使用该用户名注册").append("</message>");
                }
                out.println(builder.toString());
                System.out.println(builder.toString());

            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
